package com.sunbo509.www.gridview.smooth;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.PolylineOptions;
import com.sunbo509.www.gridview.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SmoothActivity extends Activity {

    private MapView mapView;
    private AMap aMap;
    private SmoothMarker smoothMarker;
    private float speed = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smooth);

        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        init();
    }

    /**
     * 初始化AMap对象
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();

    }

    public void move(View view) {

        addPolylineInPlayGround();

        List<LatLng> points = readLatLngs();
        LatLngBounds.Builder b = LatLngBounds.builder();
        for (int i = 0; i < points.size(); i++) {
            b.include(points.get(i));
        }
        LatLngBounds bounds = b.build();
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));

        smoothMarker = new SmoothMarker(aMap);
        smoothMarker.setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.car));

        LatLng drivePoint = points.get(0);
        Pair<Integer, LatLng> pair = PointsUtil.calShortestDistancePoint(points, drivePoint);
        points.set(pair.first, drivePoint);
        List<LatLng> subList = points.subList(pair.first, points.size());

        smoothMarker.setPoints(subList);
        smoothMarker.setSpeed(60);

        aMap.setInfoWindowAdapter(infoWindowAdapter);
        smoothMarker.setMoveListener(new SmoothMarker.SmoothMarkerMoveListener() {
            @Override
            public void move(double remainDistance, double totalDistance) {
                if (infoWindowLayout != null && title != null) {
                    title.setText("距离终点还有： " + (int) remainDistance + "米\n" + "完成百分比：" + (int)
                            ((totalDistance - remainDistance) / totalDistance * 100) + "%");
                }
            }

            @Override
            public void stop() {
                Toast.makeText(SmoothActivity.this, "动画停止", Toast.LENGTH_SHORT).show();
                Log.d("MainActivity", smoothMarker.getPosition().toString());
            }

        });
        smoothMarker.getMarker().showInfoWindow();

        smoothMarker.startSmoothMove();


    }

    public void pause(View view) {
        smoothMarker.pauseMove();
    }

    public void resume(View view) {
        smoothMarker.resumeMove();
    }

    public void accelerate(View view) {
        speed += 8;
        smoothMarker.changeSpeed(speed);
    }

    AMap.InfoWindowAdapter infoWindowAdapter = new AMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {

            return getInfoWindowView(marker);
        }

        @Override
        public View getInfoContents(Marker marker) {


            return getInfoWindowView(marker);
        }
    };

    LinearLayout infoWindowLayout;
    TextView title;
    TextView snippet;

    private View getInfoWindowView(Marker marker) {
        if (infoWindowLayout == null) {
            infoWindowLayout = new LinearLayout(SmoothActivity.this);
            infoWindowLayout.setOrientation(LinearLayout.VERTICAL);
            title = new TextView(SmoothActivity.this);
            snippet = new TextView(SmoothActivity.this);
            title.setTextColor(Color.BLACK);
            snippet.setTextColor(Color.BLACK);
            infoWindowLayout.setBackgroundResource(R.drawable.infowindow_bg);

            infoWindowLayout.addView(title);
            infoWindowLayout.addView(snippet);
        }

        return infoWindowLayout;
    }

    private void addPolylineInPlayGround() {
        List<LatLng> list = readLatLngs();
        List<Integer> colorList = new ArrayList<Integer>();
        List<BitmapDescriptor> bitmapDescriptors = new ArrayList<BitmapDescriptor>();

        int[] colors = new int[]{Color.argb(255, 0, 255, 0), Color.argb(255, 255, 255, 0), Color
                .argb(255, 255, 0, 0)};

        //用一个数组来存放纹理
        List<BitmapDescriptor> textureList = new ArrayList<BitmapDescriptor>();
        textureList.add(BitmapDescriptorFactory.fromResource(R.drawable.custtexture));

        List<Integer> texIndexList = new ArrayList<Integer>();
        texIndexList.add(0);//对应上面的第0个纹理
        texIndexList.add(1);
        texIndexList.add(2);

        Random random = new Random();
        for (int i = 0; i < list.size(); i++) {
            colorList.add(colors[random.nextInt(3)]);
            bitmapDescriptors.add(textureList.get(0));

        }

        aMap.addPolyline(new PolylineOptions().setCustomTexture(BitmapDescriptorFactory
                .fromResource(R.drawable.custtexture)) //setCustomTextureList(bitmapDescriptors)
                //				.setCustomTextureIndex(texIndexList)
                .addAll(list)
                .useGradient(true)
                .width(18));
    }

    private List<LatLng> readLatLngs() {
        List<LatLng> points = new ArrayList<LatLng>();
        for (int i = 0; i < coords.length; i += 2) {
            points.add(new LatLng(coords[i + 1], coords[i]));
        }
        return points;
    }


    /**
     * 测试在两个坐标中间任意位置停止动画，将坐标间隔变大
     */


//    private double[] coords = {121.550998, 29.867865,
//            121.5525, 29.872089,
//            121.556835, 29.871,
//            121.556674, 29.868721,
//            121.555858, 29.867344,
//            121.554131, 29.869614
//    };


    private double[] coords = {121.535280,38.890610 ,
            121.543118, 38.903712,
            121.543115, 38.913611,
            121.543011, 38.913510,
            121.543010, 38.923001
    };

}
